#!/bin/bash

# CONFIGURE DEFAULTS
export PREFIX="/usr/local"
export CTORDIR=
export WGETDIR=
export MTSTART="mediatomb_start"
export MTSTOP="mediatomb_stop"
export PIDFILE="/var/run/udm.pid"
export TMPFILE="/tmp/udm.conf"
export DOWNDIR="/usr/local/downloads"
export CPORT=8888
export LOGFILE="/tmp/udm.log"
export USER="root"
export YEAR=$(date +%Y)
export DWWW=www
export DFB=udmfb
export ADDPATH=

# COMPILER & CO
test ! $CC && export CC=gcc
test ! $CC && export CFLAGS=-O
test ! $CC && export LDFLAGS=-s
test ! $STRIP && export STRIP=strip

# READ VERSION
VERSION=`grep SERVER_VERSION udm-version.h`
VERSION=${VERSION#*\"}
export VERSION=${VERSION%\"*}
export MAJOR=$VERSION

EMAIL=`grep EMAIL udm-version.h`
EMAIL=${EMAIL#*\"}
export EMAIL=${EMAIL%\"*}

SOFTWARE=`grep SERVER_SOFTWARE udm-version.h`
SOFTWARE=${SOFTWARE#*\"}
export SOFTWARE=${SOFTWARE%\"*}

AUTHOR=`grep AUTHOR udm-version.h`
AUTHOR=${AUTHOR#*\"}
export AUTHOR=${AUTHOR%\"*}

URL=`grep SERVER_URL udm-version.h`
URL=${URL#*\"}
export URL=${URL%\"*}

function usage {
	echo \`configure\' configures $SOFTWARE $VERSION to adapt to many kinds of systems.
	echo
	echo WARNING: THERE IS NO WARRANTY FOR $SOFTWARE. USE AT YOUR OWN RISK!!!
	echo
	echo Usage: ./configure [OPTION]... [VAR=VALUE]...
	echo
	echo To assign environment variables \(e.g., CC, CFLAGS...\), specify them as
	echo VAR=VALUE.  See below for descriptions of some of the useful variables.
	echo
	echo Defaults for the options are specified in brackets.
	echo
	echo Configuration:
	echo   -h, --help              display this help and exit
	echo   -V, --version           display version information and exit
	echo
	echo Installation directories:
	echo   --prefix=PREFIX          install architecture-independent files in PREFIX [$PREFIX]
	echo
	echo By default, \`make install\' will install all the files in
	echo \`$PREFIX/$SOFTWARE-$VERSION\'.  You can specify an installation prefix other
	echo than \`$PREFIX\' using \`--prefix\', for instance \`--prefix=\$HOME\'.
	echo
	echo For better control, use the options below.
	echo
	echo Fine tuning of the installation directories:
	echo   --download-dir=DOWNDIR   determine the target download directory [${DOWNDIR#*=}]
	echo   --ctor-dir=CTORDIR       determine the position of the ctorrent executable
	echo                            if no parameter is specified the executable is searched
        echo                            first in PREFIX/$SOFTWARE-$VERSION/bin, then in \$PATH
	echo   --wget-dir=WGETDIR       determine the position of the wget executable
	echo                            if no parameter is specified the executable is searched
        echo                            first in PREFIX/$SOFTWARE-$VERSION/bin, then in \$PATH
	echo   --add-path=ADDPATH       add this paths to the PATH during CGI executables. If
	echo                            you plan to use aMule, you may want to add its path here
	echo                            if $SOFTWARE cannot find it
	echo
	echo Optional Features:
	echo     --user=USER            parameter -u for mini_httpd [$USER]
	echo     --pidfile=PIDFILE      where to save pid info of $SOFTWARE [$PIDFILE]
	echo     --tmpfile=TMPFILE      where to save temporary configuration file [$TMPFILE]
	echo     --debug[=LOGFILE]      save debug info in LOGFILE [default off, [LOGFILE=$LOGFILE]]
	echo     --sub-version=SUBV     add a trailing subversion to the official version \(useful for packaging\)
	echo     --port=CPORT           tcp/ip port $SOFTWARE will respond [$CPORT]
	echo
	echo Some influential environment variables:
	echo   CC          C compiler command
	echo   CFLAGS      C compiler flags
	echo   LDFLAGS     linker flags, e.g. -L\<lib dir\> if you have libraries in a
	echo               nonstandard directory \<lib dir\>
	echo   LIBS        libraries to pass to the linker, e.g. -l\<library\>
	echo
	echo Use these variables to override the choices made by \`configure\' or to help
	echo it to find libraries and programs with nonstandard names/locations.
	echo
	echo Report bugs to <umbacos <http://www.ilpuntotecnicoeadsl.com/forum>>.
}

sed -i "s|^#define DEBUG|//#define DEBUG|g" udm-common.h

while [ "$1" != "" ]; do
	case ${1%=*} in
	--enable-static )
		export LDFLAGS="$LDFLAGS -static"
				;;
	--prefix )
		export PREFIX=${1#*=}
				;;
	--sub-version )
		export VERSION=$VERSION-${1#*=}
				;;
	--ctor-dir )
		export CTORDIR=${1#*=}
				;;
	--wget-dir )
		export WGETDIR=${1#*=}
				;;
	--user )
		export USER="${1#*=}"
				;;
	--pidfile )
		export PIDFILE=${1#*=}
				;;
	--tmpfile )
		export TMPFILE=${1#*=}
				;;
	--download-dir )
		export DOWNDIR="${1#*=}"
				;;
	--add-path )
		export ADDPATH="${1#*=}"
				;;
	--port )
		export CPORT="${1#*=}"
				;;
	--debug )
		sed -i "s|^//#define DEBUG|#define DEBUG|g" udm-common.h
		sed -i "s|^#define TMP_CNF.*$|#define TMP_CNF \"$TMPFILE\"|g" udm-common.h
		sed -i "s|^#define LOGFILE.*$|#define LOGFILE \"$LOGFILE\"|g" udm-common.h
		test ${1%=*} != ${1#*=} && export LOGFILE="${1#*=}"
		echo "***************"
		echo "***************"
		echo "***  DEBUG  ***"
		echo "***************"
		echo "***************"
				;;
	-h | --help )
		usage
		exit
				;;
	-V | --version )
		echo $SOFTWARE $VERSION
		echo $URL
		exit
				;;
	CC )
		export CC=${1#*=}
				;;
	CFLAGS )
		export CFLAGS=${1#*=}
				;;
	LDFLAGS )
		export LDFLAGS=${1#*=}
				;;
	LIBS )
		export LIBS=${1#*=}
				;;
	* )
		echo unknown option $1
		echo use -h or --help option to display help
		exit 1
				;;
    esac
    shift
done

echo Configuration utility for $SOFTWARE $VERSION
echo writing config ...
echo CC="$CC"
echo CC="$CC" > config
echo CFLAGS="$CFLAGS"
echo CFLAGS="$CFLAGS" >> config
echo LDFLAGS="$LDFLAGS"
echo LDFLAGS="$LDFLAGS" >> config
echo LDLIBS="$LIBS"
echo LDLIBS="$LIBS" >> config
echo STRIP="$STRIP"
echo STRIP="$STRIP" >> config
echo BINDIR="$PREFIX"
echo BINDIR="$PREFIX" >> config
echo VERSION="$VERSION"
echo VERSION="$VERSION" >> config
echo SOFTWARE="$SOFTWARE"
echo SOFTWARE="$SOFTWARE" >> config
echo AUTHOR="$AUTHOR"
echo AUTHOR="$AUTHOR" >> config
echo MAJOR="$MAJOR"
echo MAJOR="$MAJOR" >> config
echo EMAIL="$EMAIL"
echo EMAIL="$EMAIL" >> config
echo URL="$URL"
echo URL="$URL" >> config
echo CTORDIR="$CTORDIR"
echo CTORDIR="$CTORDIR" >> config
echo WGETDIR="$WGETDIR"
echo WGETDIR="$WGETDIR" >> config
echo MTSTART="$MTSTART"
echo MTSTART="$MTSTART" >> config
echo MTSTOP="$MTSTOP"
echo MTSTOP="$MTSTOP" >> config
echo PIDFILE="$PIDFILE"
echo PIDFILE="$PIDFILE" >> config
echo USER="$USER"
echo USER="$USER" >> config
echo DOWNDIR="$DOWNDIR"
echo DOWNDIR="$DOWNDIR" >> config
echo CPORT="$CPORT"
echo CPORT="$CPORT" >> config
echo LOGFILE="$LOGFILE"
echo LOGFILE="$LOGFILE" >> config
echo YEAR="$YEAR"
echo YEAR="$YEAR" >> config
echo DWWW="$DWWW"
echo DWWW="$DWWW" >> config
echo DFB="$DFB"
echo DFB="$DFB" >> config
echo ADDPATH="$ADDPATH"
echo ADDPATH="$ADDPATH" >> config
echo writing Makefile ...
cat Makefile.am > Makefile
echo Done.
